package com.prs.crm.action.flow;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.flow.WorkFlowType;
import com.prs.crm.service.flow.WorkFlowTypeService;
import com.prs.crm.service.sys.LogService;

public class WorkFlowTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private WorkFlowTypeService workflowTypeService;

	private PaginationSupport pagination;

	private WorkFlowType type;

	private Integer[] typeIds;

	private TreeNode node;
	private LogService logService;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public void setWorkflowTypeService(WorkFlowTypeService workflowTypeService) {
		this.workflowTypeService = workflowTypeService;
	}

	public WorkFlowTypeService getWorkflowTypeService() {
		return workflowTypeService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setType(WorkFlowType type) {
		this.type = type;
	}

	public WorkFlowType getType() {
		return type;
	}

	public void setNode(TreeNode node) {
		this.node = node;
	}

	public TreeNode getNode() {
		return node;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	@Action("listType")
	public String listType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				WorkFlowType.class);
		this.setPagination(this.getWorkflowTypeService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editType")
	public String editType() {
		return SUCCESS;
	}

	@Action(value = "saveType", results = {
			@Result(name = "success", type = "redirect", location = "listType"),
			@Result(name = "error", type = "chain", location = "editType") })
	public String saveType() throws SQLException {
		if (type.getName() == null || "".equals(type.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}if (this.getLogService().getSearchName(WorkFlowType.class, type
				.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getWorkflowTypeService().save(type);
//		this.saveEimWorkFlowType(type);
		return SUCCESS;
	}

	@Action(value = "removeType", results = { @Result(name = "success", type = "redirect", location = "listType") })
	public String removeType() {
		this.getWorkflowTypeService().batchRemove(typeIds);
		return SUCCESS;
	}

	@Action("typeTree")
	public String typeTree() {
		return SUCCESS;
	}

	@Action(value = "loadTypeNodes", results = { @Result(name = "success", type = "json", params = {
			"root", "node" }) })
	public String loadTypeNodes() {
		node = new TreeNode();
		node.setId("-1");

		TreeNode rootNode = new TreeNode();
		rootNode.setId("0");
		rootNode.setText("流程类型");

		for (WorkFlowType type : this.getWorkflowTypeService().getAllTypes()) {
			TreeNode typeNode = new TreeNode();
			typeNode.setId(type.getId().toString());
			typeNode.setText(type.getName());
			rootNode.addChildNode(typeNode);
		}

		node.addChildNode(rootNode);
		return SUCCESS;
	}
	public void saveEimWorkFlowType(WorkFlowType type) throws SQLException{
		Properties pro=new Properties();
		String path = Thread.currentThread().getContextClassLoader().getResource("connect.properties").getPath();
		try {
			pro.load(new FileInputStream(path));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String ClassName=pro.getProperty("ClassName");
		String userName=pro.getProperty("userName");
		String userPass=pro.getProperty("userPass");
		String url1=pro.getProperty("url1");
		try {
			Class.forName(ClassName);
			con = DriverManager.getConnection(url1, userName, userPass);
			stmt = this.con.createStatement();
			rs=stmt.executeQuery("select * from flow_type where name='"+type.getName()+"'");
			while (!rs.next()) {
				String sql="insert into flow_type(name) values('"+type.getName()+"')";
				stmt.executeUpdate(sql);
			}
			con.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
